/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ins.entity.InsData;
import com.thinkgem.jeesite.modules.ins.entity.InsLog;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
import com.thinkgem.jeesite.modules.ins.service.InsLogService;

/**
 * 检验报告书-日志Controller
 * @author fanqinglong
 * @version 2017-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insLog")
public class InsLogController extends BaseController {

	@Autowired
	private InsLogService insLogService;
	
	@ModelAttribute
	public InsLog get(@RequestParam(required=false) String id) {
		InsLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insLogService.get(id);
		}
		if (entity == null){
			entity = new InsLog();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(InsLog insLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InsLog> page = insLogService.findPage(new Page<InsLog>(request, response), insLog); 
		model.addAttribute("insLog", insLog);
		model.addAttribute("page", page);
		return "modules/ins/insLogList";
	}

	@RequiresPermissions("ins:insLog:view")
	@RequestMapping(value = "form")
	public String form(InsLog insLog, Model model,HttpServletRequest request) {
		model.addAttribute("insLog", insLog);
		JsonMapper jsonMapper = JsonMapper.getInstance();
		//修改
		if (InsLog.TYPE_SAVE.equals(insLog.getType())) {
			//修改表
			if (InsLog.MODULE_TABLE.equals(insLog.getModule())) {
				Map<String, InsTable> map = jsonMapper.fromJson(insLog.getData(), jsonMapper.createCollectionType(HashMap.class, String.class,InsTable.class));
				InsTable oldTable = map.get("oldTable");
				InsTable insTable = map.get("insTable");
				model.addAttribute("insTable", insTable);
				if ("before".equals(request.getParameter("save"))) {
					model.addAttribute("insTable", oldTable);
					return "modules/ins/insTableSaveDisableBefore";
				}
				return "modules/ins/insTableSaveDisableAfter";
			}else {
				Map<String, InsData> map = jsonMapper.fromJson(insLog.getData(), jsonMapper.createCollectionType(HashMap.class, String.class,InsData.class));
				InsData oldData = map.get("oldData");
				InsData insData = map.get("insData");
				model.addAttribute("insData", insData);
				if ("before".equals(request.getParameter("save"))) {
					model.addAttribute("insData", oldData);
					return "modules/ins/insDataSaveDisableBefore";
				}
				return "modules/ins/insDataSaveDisableAfter";
			}
		}
		//数据表的日志
		if (InsLog.MODULE_TABLE.equals(insLog.getModule())) {
			InsTable insTable = jsonMapper.fromJson(insLog.getData(), InsTable.class);
			model.addAttribute("insTable", insTable);
			if (InsLog.TYPE_ASSIGN_AUTH.equals(insLog.getType())) {
				return "modules/ins/insTableAssignDisable";
			}
			return "modules/ins/insTableDisable";
		}else {
			InsData insData = jsonMapper.fromJson(insLog.getData(), InsData.class);
			model.addAttribute("insData", insData);
			return "modules/ins/insDataDisable";
		}
	}
	
	@RequiresPermissions("ins:insLog:view")
	@RequestMapping(value = "export")
	public String export(InsLog insLog,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		boolean flag = "true".equals(request.getParameter("pagination"));
		Page<InsLog> page = new Page<>(request,response);
		try {
			String url = insLogService.exportPDF(request, insLog, page, flag);
			File file = new File(url);
			return FileUtils.downFile(file, request, response);
		} catch (IOException e) {
			addMessage(redirectAttributes, "导出数据失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/ins/insLog/list?repage";
	}
}